// pages/my/my.js
const app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
    // banner: app.data.imgurl +"banner.png",   // 背景图
    headsrc: app.data.imgurl +"judgess.png",  // 头像
    reddot: app.data.imgurl +"reddot.png",    // 红点
    message: app.data.imgurl +"messge.png",   // 消息
    system:  app.data.imgurl +"systems.png",  //设置
    crown: app.data.imgurl +"crown.png", // 皇冠会员
    photo: app.data.imgurl +"eyes.png",   // 眼睛
    chat:  app.data.imgurl +"chat.png",   // 评论
    fabulous:  app.data.imgurl +"fabulous.png", //赞
    indexd:"0",                       //首先
    mys:[
      {number:"2",text:"作品",child:[]},
      { number: "2", text: "参赛", child: []},
      { number: "2", text: "喜欢", child: []},
      { number: "2", text: "下载", child: []}
    ],
    muwork:[
      { src: app.data.imgurl +"banner.png",number1:"100",number2:"200",number3:"300"},
      { src:  app.data.imgurl +"banner.png", number1: "100", number2: "200", number3: "300" },
      { src:  app.data.imgurl +"banner.png", number1: "100", number2: "200", number3: "300" },
      { src:  app.data.imgurl +"banner.png", number1: "100", number2: "200", number3: "300" }
    ],
    getuserinfo: app.data.url + 'sheyingjia/Xmyuser1/getuserinfo',
    gettoken: app.data.url + 'sheyingjia/Xuploas1/gettoken',
    addenclosure: app.data.url + 'sheyingjia/Xuploas1/addenclosure',
    changefenmian: app.data.url + 'sheyingjia/Xmyuser1/changefenmian',
    getmyuserzuopinlist: app.data.url + 'sheyingjia/Xpage1/getmyuserzuopinlist',
    getmyusercansailist: app.data.url + 'sheyingjia/Xpage1/getmyusercansailist',
    getmyuserxifanlist: app.data.url + 'sheyingjia/Xpage1/getmyuserxifanlist',
    getmyuserxihuanlist: app.data.url + 'sheyingjia/Xpage1/getmyuserxihuanlist',
    deletes: app.data.url + 'sheyingjia/xzuoping1/deletes',
    myshuju:{},
    zuopinpage:1,
    cansaipage:1,
    xihuanpage:1,
    xiazaipage:1,

  },
    //导航栏切换
  qiehuan:function(e){
    var index = e.currentTarget.dataset.index;
     this.setData({
     indexd:index
     })

  },
  setup:function(){
  
    wx.navigateTo({
      url: '/pages/setup/setup',
    })
  },
  tosehzhessd:function(){
    wx.navigateTo({
      url: '/pages/setup/setup',
    })

  },

  //  去我的钱包
  tomywallet:function(){
wx.navigateTo({
  url: '/pages/mywallet/mywallet',
})

  },
  //去我的消息
  tomessage:function(){
    wx.navigateTo({
      url: '/pages/messagenotification/messagenotification',
    })
  },
  //作品详情
  toworksd:function(e){
    console.log(e);
    var url = e.currentTarget.dataset.url;
    // return
    wx.navigateTo({
      url: url,
    })
  },
  //修改封面
  huanfenmian:function(){
    var that = this;
    // wx.showLoading({
    //   title: '上传中',
    // })
    ea.showToUp('img',1,function(res){
      console.log('全部')
      wx.hideLoading();
      // console.log(quanbu);
      var datas = res.data;
      if(datas.code==200){
        var user = wx.getStorageSync('user');
        console.log(datas.data[0].id);
        ea.request(that.data.changefenmian, { fenmian: datas.data[0].id, token: user.token }, function (ress) {
          var data = ress.data;
          if(data.code==200){
            that.onShow();
          }else{
            wx.showToast({
              title: data.msg,
              icon:'none'
            })
          }
        })
      }else{
        wx.showToast({
          title: datas.msg,
          icon:'none'
        })
      }
    },function(e){
      console.log(e.progress);
      var str = e.progress;
      if(str==100){
        str =99;
      }
      str = str.toString();
      wx.showLoading({
        title: str+'%',
      })

    });
  },
  //删除数据
  deletes:function(e){
    console.log(e);
    var id = e.currentTarget.dataset.id;
    var indexd = this.data.indexd;
    var user = wx.getStorageSync('user');
    var that = this;
    // console.log(indexd);
    if (indexd==3){
      wx.showModal({
        title: '提示',
        content: '删除后再次下载需要重新购买?',
        success: function (e) {
          if (e.confirm) {
            ea.qinqiu(that.data.deletes, { id: id, type: indexd, token: user.token }, function (data) {
              console.log(data);
              that.onShow();
            })
          }
        }
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '你确定要删除吗?',
        success: function (e) {
          if (e.confirm) {
            ea.qinqiu(that.data.deletes, { id: id, type: indexd, token: user.token }, function (data) {
              console.log(data);
              that.onShow();
            })
          }
        }
      })
    }
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('user');
    var that = this;
    if (!user) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (tye) {
    var user = wx.getStorageSync('user');
    var that = this;
    // if (!user) {
    //   wx.navigateTo({
    //     url: '/pages/login/login',
    //   })
    // }
    ea.request(that.data.getuserinfo, { token: user.token }, function (res) { 
      var data = res.data;
      console.log(data.data);
      if(data.code==200){
        that.setData({myshuju:data.data});
      }else{
        wx.showToast({
          title: data.msg,
          icon:'none'
        })
      }
    })
    this.getzuopin();
    this.getcansai();
    this.getxihuan();
    this.getxiazai();

  },
  //获取作品
  getzuopin:function(){
    var page = this.data.zuopinpage;
    var user = wx.getStorageSync('user');
    var that = this;
    // if(page==0){
    //   wx.showToast({
    //     title: '没有更多了',
    //     icon:'none'
    //   })
    //   return;
    // }
    ea.request(that.data.getmyuserzuopinlist,{page:page,token:user.token},function(res){
      var data = res.data;
      console.log(data);
      if(data.code==200){
        if(data.data.length<9){
          that.setData({zuopinpage:0});
        }else{
          that.setData({ zuopinpage: page+1 });
        };
        var mys = that.data.mys;
        var child = [];
        ea.each(data.data,function(i,v){
          v.wurl = '/pages/pxqworks/pxqworks?id='+v.id;
          if(!v.fenmian){
            v.fenmian = v.videos +'?vframe/jpg/offset/1';
          }
          child.push(v);
        })
        // child = ea.addArray(child,data.data);
        mys[0].child = child;
        console.log(mys);
        that.setData({ mys: mys});
      }else{
        wx.showToast({
          title: data.msg,
          icon:'none'
        })
      }
    })
  },
  //获取比赛
  getcansai:function(){
    var page = this.data.cansaipage;
    var user = wx.getStorageSync('user');
    var that = this;
    // if (page == 0) {
    //   wx.showToast({
    //     title: '没有更多了',
    //     icon: 'none'
    //   })
    //   return;
    // }
    ea.request(that.data.getmyusercansailist,{page:page,token:user.token},function(res){
      var data = res.data;
      // console.log(data);
      if (data.code == 200) {
        if (data.data.length < 9) {
          that.setData({ cansaipage: 0 });
        } else {
          that.setData({ cansaipage: page + 1 });
        };
        var mys = that.data.mys;
        var child = [];
        ea.each(data.data, function (i, v) {
          v.wurl = '/pages/xqworks/xqworks?id=' + v.id;
          if (!v.fenmian) {
            v.fenmian = v.videos + '?vframe/jpg/offset/1';
          }
          child.push(v);
        })
        mys[1].child = child;
        // console.log(mys);
        that.setData({ mys: mys });
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    })

  },
  //获取喜欢
  getxihuan:function(){
    var page = this.data.xihuanpage;
    var user = wx.getStorageSync('user');
    var that = this;
    // if (page == 0) {
    //   wx.showToast({
    //     title: '没有更多了',
    //     icon: 'none'
    //   })
    //   return;
    // }
    ea.request(that.data.getmyuserxifanlist, { page: page, token: user.token }, function (res) {
      var data = res.data;
      // console.log(data);
      if (data.code == 200) {
        if (data.data.length < 9) {
          that.setData({ xihuanpage: 0 });
        } else {
          that.setData({ xihuanpage: page + 1 });
        };
        var mys = that.data.mys;
        var child = [];
        ea.each(data.data, function (i, v) {
          v.wurl = '/pages/pxqworks/pxqworks?id=' + v.zuopin.id;
          child.push(v);
        })
        mys[2].child = child;
        // console.log(mys);
        that.setData({ mys: mys });
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    })
  },
  //获取我的下载
  getxiazai:function(){
    var page = this.data.xiazaipage;
    var user = wx.getStorageSync('user');
    var that = this;
    // if (page == 0) {
    //   wx.showToast({
    //     title: '没有更多了',
    //     icon: 'none'
    //   })
    //   return;
    // }
    ea.request(that.data.getmyuserxihuanlist, { page: page, token: user.token }, function (res) {
      var data = res.data;
      // console.log(data);
      if (data.code == 200) {
        if (data.data.length < 9) {
          that.setData({ xiazaipage: 0 });
        } else {
          that.setData({ xiazaipage: page + 1 });
        };
        var mys = that.data.mys;
        var child = [];
        ea.each(data.data, function (i, v) {
          // v.wurl = '/pages/pxqworks/pxqworks?id=' + v.zuopin.id;
          child.push(v);
        })
        mys[3].child = child;
        // console.log(mys);
        that.setData({ mys: mys });
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})